package com.example.demo.DAO.myBatisDao.impl;


import com.example.demo.DAO.myBatisDao.ICacheDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author:TobbyQuinn
 * @date:2018/3/1
 * @project:studyboot
 **/
@Component
public class RedisDao implements ICacheDao {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisClusterConfiguration redisClusterConfiguration;

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean setString(String key, String value, int expireTime) {
        try {
            redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String getString(String key) {

        Object value = redisTemplate.opsForValue().get(key);
        if (value instanceof String) {
            return (String) value;
        } else {
            throw new ClassCastException("类型转换异常");
        }
    }


    @Override
    public boolean setObject(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Object getObject(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public boolean delete(String key) {

        try {
            redisTemplate.delete(key);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public boolean delete(Collection<Object> keys) {
        try {
            redisTemplate.delete(keys);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public boolean flushDB(RedisClusterNode redisClusterNode) {

        try {
            redisTemplate.opsForCluster().flushDb(redisClusterNode);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public boolean flushAll() {
        try {
            Set<RedisNode> clusterNodes = redisClusterConfiguration.getClusterNodes();
            for (RedisNode redisNode : clusterNodes) {
                String redisHost = redisNode.getHost();
                Integer redisPort = redisNode.getPort();
                logger.info("flushDB节点: {}:{}",redisHost,redisPort);
                if (logger.isDebugEnabled()) {
                    logger.debug("执行FlushDB操作，被执行节点为：{}:{}",redisHost,redisPort );
                }
                RedisClusterNode redisClusterNode = new RedisClusterNode(redisHost, redisPort);
                System.out.println(redisClusterNode.getType());
                if(redisClusterNode.isMaster()){
                    redisTemplate.opsForCluster().flushDb(new RedisClusterNode( redisHost,redisPort));
                    if (logger.isDebugEnabled()) {
                        logger.debug("执行FlushDB操作成功，被执行节点为：{}:{}",redisHost,redisPort );
                    }
                }
            }
            return true;
        } catch (Exception e) {
            throw e;
        }
    }
}
